/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "default_job_queue.h"
#include "log.h"

namespace ImageKnifePro {
size_t DefaultJobQueue::GetQueueLength()
{
    return highQueue_.size() + highQueue_.size() + normalQueue_.size() + lowQueue_.size();
}

void DefaultJobQueue::Add(Job job)
{
    IMAGE_KNIFE_CHECK(job.request == nullptr, "NUll ImageKnifeRequest");
    if (job.type == ImageRequestType::PLACE_SRC) {
        placeholder_.push(job);
        return;
    }

    if (job.request->GetImageKnifeOption()->priority == Priority::HIGH) {
        highQueue_.push(job);
    } else if (job.request->GetImageKnifeOption()->priority == Priority::MEDIUM) {
        normalQueue_.push(job);
    } else {
        lowQueue_.push(job);
    }
}

DefaultJobQueue::Job DefaultJobQueue::Pop()
{
    if (!placeholder_.empty()) {
        return PopFromQueue(placeholder_);
    }

    if (!highQueue_.empty()) {
        return PopFromQueue(highQueue_);
    }

    if (!normalQueue_.empty()) {
        return PopFromQueue(normalQueue_);
    }

    if (!lowQueue_.empty()) {
        return PopFromQueue(lowQueue_);
    }

    return Job{};
}

DefaultJobQueue::Job DefaultJobQueue::PopFromQueue(std::queue<Job> &queue)
{
    Job job = queue.front();
    queue.pop();
    return job;
}

}